#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SHM_SIZE 1024

int main() {
    int shmid;
    key_t key = ftok(".", 'M');
    char *shm_addr;
    
    /* 创建共享内存段 */
    shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
    
    /* 附加到进程地址空间 */
    shm_addr = shmat(shmid, NULL, 0);
    
    /* 写入共享数据 */
    strcpy(shm_addr, "Hello World!");
    printf("i have got from shared memory:%s\n", shm_addr);
    
    /* 分离共享内存 */
    shmdt(shm_addr);
    
    /* 删除共享内存段 */
    shmctl(shmid, IPC_RMID, NULL);
    return 0;
}
